package com.example.live.repo

import com.example.common.repo.BaseRepository
import com.example.http.OkHttpClientManager
import com.example.http.RetrofitManager
import com.example.live.api.LiveServerApi
import com.example.live.entity.GoodsEntity
import com.example.live.entity.LiveEntityItem
import com.example.live.entity.UserEntity
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow

class LiveRepository : BaseRepository<LiveServerApi>() {
    override fun createApiServer(): LiveServerApi {
        return RetrofitManager.getRetrofit(OkHttpClientManager.createOkHttpClient()).create(LiveServerApi::class.java)
    }
    suspend fun getRecommendVideo(page: Int, pageSize: Int): Flow<List<LiveEntityItem>> {
        return flow {
            var result = apiServer.getRecommendSimpleVideo(page, pageSize)
            if (result.code == 200){
                emit(result.data)
            }else{
                emit(listOf())
            }
        }
    }
    suspend fun getGoodsInfo(category_id:Int, currentPage: Int, pageSize:Int, room_id:String): Flow<List<GoodsEntity>> {
        return flow {
            var result = apiServer.getGoodsInfo(category_id, currentPage,pageSize,room_id)
            if (result.code == 200){
                emit(result.data)
            }else{
                emit(listOf())
            }
        }
    }
    suspend fun userLiveRoom():Flow<List<UserEntity>>{
        return flow {
            val result = apiServer.getUser()
            if (result.code == 200){
                emit(result.data)
            }else{
                emit(listOf())
            }
        }
    }
}